import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store/index'
import login from '../views/UserLogin';

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: login,
    meta: { auth: true, title: '登录' },
    // redirect: () => import('../views/UserLogin.vue')
  },
  {
    path: '/login',
    component: login,
    meta: { auth: true, keepAlive: false, title: '登录' },
    name: 'Login'
  },
  {
    path: '/patrol-staff',
    component: () => import('../views/PatrolStaff.vue'),
    meta: { auth: true, keepAlive: false, title: '巡防员' },
    name: 'PatrolStaff'
  },
  {
    path: '/assayer',
    component: () => import('../views/AnalystStaff.vue'),
    meta: { auth: true, keepAlive: false, title: '化验员' },
    name: 'AnalystStaff',
    children: [
      {
        path: '',
        redirect: 'home'
      },
      {
        path: 'home',
        component: () => import('../views/assay/HomePage.vue'),
      },
      {
        path: 'PhyChem',
        component: () => import('../views/assay/PhysicoChemical.vue'),
      },
      {
        path: 'metal',
        component: () => import('../views/assay/MetalPage.vue'),
      },
      {
        path: 'organic',
        component: () => import('../views/assay/OrganicPage.vue'),
      },
    ]
  },
  {
    path: '/provincial',
    component: () => import('../views/ProvincialLevel.vue'),
    meta: { auth: true, keepAlive: false, title: '省市级监测调度' },
    name: 'provincial'
  },
  {
    path: '/national',
    component: () => import('../views/NationalLevel.vue'),
    meta: { auth: true, keepAlive: false, title: '国家海洋监测调度' },
    name: 'national'
  },
  {
    path: '/seeAllData',
    component: () => import('../views/SeeAllData.vue'),
    meta: { auth: true, keepAlive: false, title: '海洋监测各项指标' },
    name: 'seeAllData'
  },
  {
    path: '/exportAllData',
    component: () => import('../views/ExportAllData.vue'),
    meta: { auth: true, keepAlive: false, title: '海洋监测各项指标' },
    name: 'exportAllData'
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  document.title = to.matched[0].meta.title
  if (to.matched.some(m => m.meta.auth)) {
    // console.log(from.name)
    if (to.name == 'Login') {
      next();
    } else {
      //判断是否已经登录
      if (JSON.stringify(store.state.userInfo) === "{}") {
        next('/Login');
      } else {
        next()
      }
    }
  }

  // 权限不同禁止跳转 目前是巡防员可以跳转到化验员 因为详细信息只查点位，点位编码
})

export default router
